<template>
  <div class="dragger-warp">
    <div class="title">
      <slot name="title-left"
            class="title-left"></slot>
      <div class="title-right">
        <i class="el-icon-rank move-icon"></i>
        <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link">
            <el-button type="text"
                       icon="el-icon-more"></el-button>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="1">刷新</el-dropdown-item>
            <el-dropdown-item divided
                              command="2">移除区块</el-dropdown-item>
            <el-dropdown-item divided
                              command="3">自定义视图</el-dropdown-item>
            <el-dropdown-item divided
                              command="4">恢复默认布局</el-dropdown-item>
            <el-dropdown-item v-if="obj.uniqueCode === 'quickEntry'"
                              divided
                              command="5">编辑快捷入口</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <VueDraggableResizable class="content"
                           :draggable="false"
                           :minHeight="obj.uniqueCode === 'workSum'?160:250"
                           :h="obj.height"
                           :handles="['bm']"
                           :onResize="onResize">
      <slot name="content"></slot>
    </VueDraggableResizable>
  </div>
</template>

<script>
import VueDraggableResizable from 'vue-draggable-resizable'
import 'vue-draggable-resizable/dist/VueDraggableResizable.css'

export default {
  components: { VueDraggableResizable },
  props: {
    width: {
      type: [String, Number],
      default: '33%'
    },
    obj: {
      type: Object,
      required: true
    }
  },
  mounted () {
    this.$emit('getData')
  },
  data () {
    return {
      timer: null
    }
  },
  methods: {
    handleCommand (command) {
      if (command === '1') this.$emit('getData')
      else if (command === '2') this.$emit('delete-area')
      else if (command === '3') this.$emit('change-area')
      else if (command === '4') this.$emit('default-layout')
      else this.$emit('update-button')
    },
    onResize () {
      if (this.timer) clearTimeout(this.timer)
      this.timer = setTimeout(() => { this.$emit('update-area', arguments[4]) }, 1000);
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  width: 100% !important;
  position: relative !important;
  border: none;
  height: calc(100% - 20px);
}
.dragger-warp {
  width: calc(100% - 30px);
  background: #fff;
  border-radius: 4px;
  margin: 5px 5px 10px 5px;
  min-width: 350px;
  padding: 10px 10px 0 10px;
  break-inside: avoid;
  /deep/.handle-bm {
    bottom: 0;
    width: calc(100% + 22px);
    left: -6px;
    border: none;
    display: flex !important;
    flex-flow: row nowrap;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: all 0.2s;
    overflow: hidden;
    height: 16px;
    z-index: 999;
    &:hover {
      opacity: 1;
      &::after {
        bottom: 0;
      }
    }
    &::after {
      content: "\e775";
      font-family: "iconfont" !important;
      font-size: 16px;
      font-style: normal;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      position: absolute;
      bottom: -20px;
      transition: all 0.2s;
    }
  }
  .title {
    width: 100%;
    height: 20px;
    display: flex;
    flex-flow: row nowrap;
    align-items: center;
    justify-content: space-between;
    &-left {
      width: 70%;
    }
    &-right {
      margin-left: 10px;
      width: 40px;
      display: flex;
      flex-flow: row nowrap;
      align-items: center;
      justify-content: space-between;
      .move-icon {
        cursor: pointer;
        color: #b3b3b3;
        font-size: 14px;
        margin-right: 10px;
      }
      .el-button {
        color: #b3b3b3;
      }
    }
  }
}
</style>